package com.antonytrupe.fatcontroller.pc;

import java.io.IOException;
import java.util.ArrayList;
import java.util.HashMap;

import javax.bluetooth.BluetoothStateException;
import javax.bluetooth.DeviceClass;
import javax.bluetooth.DiscoveryAgent;
import javax.bluetooth.DiscoveryListener;
import javax.bluetooth.LocalDevice;
import javax.bluetooth.RemoteDevice;
import javax.bluetooth.ServiceRecord;
import javax.bluetooth.UUID;
import javax.swing.JFrame;

import com.antonytrupe.fatcontroller.Engine;
import com.antonytrupe.fatcontroller.pc.ui.EngineControlPanel;
import com.antonytrupe.fatcontroller.pc.ui.EngineListPanel;
import com.antonytrupe.fatcontroller.ui.EngineListView;

public class PCController implements DiscoveryListener {

	private JFrame mainFrame;

	@Override
	public void backward(Engine engine) {
		// TODO move engine backward
	}

	@Override
	public void setEngineName(Engine engine, String name) {
		// TODO rename engine
	}

	@Override
	public void createGUI() {
		mainFrame = new JFrame("Engine EngineListView");
		mainFrame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);

		mainFrame.setVisible(true);
		mainFrame.setSize(260, 400);

		// TODO
		displayEngines(new PCSearch() {
		});
		mainFrame.pack();

	}

	@Override
	public void forward(Engine engine) {
		// TODO move engine forward
	}

	// device discovery complete
	@Override
	public void inquiryCompleted(int discType) {
		inquiryRunning = false;
		enableRefresh(true);
		System.out.println("Device Inquiry Completed.");

		switch (discType) {
		case DiscoveryListener.INQUIRY_COMPLETED:
			System.out.println("INQUIRY_COMPLETED");
			break;
		case DiscoveryListener.INQUIRY_TERMINATED:
			System.out.println("INQUIRY_TERMINATED");
			break;
		case DiscoveryListener.INQUIRY_ERROR:
			System.out.println("INQUIRY_ERROR");
			break;
		default:
			System.out.println("Unknown Response Code");
			break;
		}
	}

	/**
	 * servicesDiscovered() is called by the DiscoveryAgent when a service
	 * search finds services. transID identifies the service search that
	 * returned results. serviceRecord holds references to the services found.
	 */
	@Override
	public void servicesDiscovered(int transID, ServiceRecord[] serviceRecords) {
		System.out.println("Services Discovered for transID " + transID);
		// Keep track of discovered services, adding them
		// to a Vector
		for (ServiceRecord sr : serviceRecords) {
			String connectionURL = sr.getConnectionURL(
					ServiceRecord.NOAUTHENTICATE_NOENCRYPT, false);
			System.out.println("Connection URL:" + connectionURL);
		}
	}

	/**
	 * serviceSearchCompleted() is called by the DiscoveryAgent implementation
	 * when a service search finishes. transID identifies a particular service
	 * search. responseCode indicates why the service search ended.
	 */
	@Override
	public void serviceSearchCompleted(int transID, int responseCode) {
		// Now that the service discovery has been completed,
		// dispatch thread to handle the discovered services
		System.out.println("Service Search " + transID + " Completed.");
		switch (responseCode) {
		case INQUIRY_COMPLETED:
			System.out.println("INQUIRY_COMPLETED");
			break;
		case INQUIRY_ERROR:
			System.out.println("INQUIRY_ERROR");
			break;
		case INQUIRY_TERMINATED:
			System.out.println("INQUIRY_TERMINATED");
			break;
		case SERVICE_SEARCH_COMPLETED:
			System.out.println("SERVICE_SEARCH_COMPLETED");
			break;
		case SERVICE_SEARCH_DEVICE_NOT_REACHABLE:
			System.out.println("SERVICE_SEARCH_DEVICE_NOT_REACHABLE");
			break;
		case SERVICE_SEARCH_ERROR:
			System.out.println("SERVICE_SEARCH_ERROR");
			break;
		case SERVICE_SEARCH_NO_RECORDS:
			System.out.println("SERVICE_SEARCH_NO_RECORDS");
			break;
		case SERVICE_SEARCH_TERMINATED:
			System.out.println("SERVICE_SEARCH_TERMINATED");
			break;
		default:
			System.out.println("Response Code " + responseCode);
			break;
		}

	}

	@Override
	public void showEngineControlPanel(Engine engine) {
		mainFrame.getContentPane().removeAll();
		mainFrame.getContentPane().add(new EngineControlPanel(engine, this));
		mainFrame.getContentPane().validate();
		mainFrame.getContentPane().repaint();
	}

	@Override
	public void stop(Engine engine) {
		// TODO stop engine
	}
}